package com.example.week2_try2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity extends ListActivity implements Runnable{

    private static final String TAG = "RateListActivity";

    Handler handler;
    private ArrayList<HashMap<String,String>> listItems;
    private SimpleAdapter listItemAdaptor;//适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main5);

        handler=new Handler(Looper.myLooper()){//
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.d(TAG, "handleMessage: 收到消息");
                if(msg.what==9){
                    //提取数据
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(RateListActivity.this,
                            android.R.layout.simple_list_item_1,list2);//list_data可换list1
                    setListAdapter(adapter);


                }
                super.handleMessage(msg);
            }
        };

        //用其他方法
        MyTask myTask = new MyTask();
        myTask.setHandler(handler);    //不调用该方法会显示空指针异常
        Thread thread = new Thread(myTask);
        thread.start();


        //定义新数据格式
//        listItems = new ArrayList<HashMap<String,String>>();
//        for(int i = 0;i<10;i++){
//            HashMap<String,String> map = new HashMap<String,String>();//map一定要在循环里创建，在外面的话就是同一个map不停的放进list
//            map.put("ItemTitle","Rate: = "+i);//标题文字
//            map.put("ItemDetail","detail: = "+i);//详情描述
//            listItems.add(map);
//        }
//        //生成适配器的Item和动态数组对应的元素
//        listItemAdaptor = new SimpleAdapter(this,listItems,R.layout.activity_main5,new String[]{"ItemTitle","ItemDetail"},new int[] {R.id.itemtitle,R.id.itemDetail});

        //构造数据
//        List<String> list1 = new ArrayList<String>();
//        for(int i=1;i<100;i++){
//            list1.add("item"+i);
//        }
//        String[] list_data = {"one","two","three","four"};
//        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list_data);//list_data可换list1
//        setListAdapter(adapter);
    }

    @Override
    public void run() {

    }
}